$(function () {
    //显示模态框log内容
    $('#page_list_tr').on('click','tr',function(){
        //var content = $(this).last('td').find('span').prop('title');
        var content = $(this).find('td:last-child').find('span').prop('title');
        $('#AssetPackageEditing-approval-log').find('pre').text(content);
        $('.modal_log_show').trigger('click');
    })

    //点击确定模态框
    $('.modal_log_btn').click(function(){
        $(this).parent().parent().find('.close').trigger('click');
    })

    /****************  资产包日志分页end  *****************/
    var url = $('#log_pager').val();
    var package_id = $('#package_id').val();

    //选择日期类型
    $('select[name="date"]').change(function () {
        var value = $(this).val();
        var page = parseInt($('#current_page').val(), 10);
        var module = $('select[name="module"]').val(); //类型
        var user = $('select[name="user"]').val(); //用户
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = value;
        param.module = module;
        param.user = user;
        param.page = page;
        param.pageSize = pageSize;
        param.package_id = package_id;

        package_update_log_page(url, param, page);
    });

    //选择module
    $('select[name="module"]').change(function () {
        var value = $(this).val();
        var page = parseInt($('#current_page').val(), 10);
        var date = $('select[name="date"]').val(); //查询日期
        var user = $('select[name="user"]').val(); //用户
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = date;
        param.module = value;
        param.user = user;
        param.page = page;
        param.pageSize = pageSize;
        param.package_id = package_id;

        package_update_log_page(url, param, page);
    });

    //选择操作人
    $('select[name="user"]').change(function () {
        var value = $(this).val();
        var page = parseInt($('#current_page').val(), 10);
        var module = $('select[name="module"]').val(); //类型
        var date = $('select[name="date"]').val(); //查询日期
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = date;
        param.module = module;
        param.user = value;
        param.page = page;
        param.pageSize = pageSize;
        param.package_id = package_id;

        package_update_log_page(url, param, page);
    });

    //选择每页显示条数
    $('#list .pageTotalBox').on('click', 'li', function () {
        var value = $(this).text();
        var page = parseInt($('#current_page').val(), 10);
        var date = $('select[name="date"]').val(); //查询日期
        var module = $('select[name="module"]').val(); //类型
        var user = $('select[name="user"]').val(); //用户

        var param = {};
        param.date = date;
        param.module = module;
        param.user = user;
        param.page = page;
        param.pageSize = value;
        param.package_id = package_id;

        package_update_log_page(url, param, page);
    });

    //跳转页面
    $("#rePage").blur(function () {
        var value = $(this).val();
        var date = $('select[name="date"]').val(); //查询日期
        var module = $('select[name="module"]').val(); //类型
        var user = $('select[name="user"]').val(); //用户
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = date;
        param.module = module;
        param.user = user;
        param.page = value;
        param.pageSize = pageSize;
        param.package_id = package_id;

        package_update_log_page(url, param, value);
    });

    //选择页面
    $("body").delegate("#page_list .pagination a", "click", function (e) {
        e.preventDefault();
        var cs = $(this).attr('href').split('?');
        //var cs = $(this).attr('href');
        //var urlParam = cs.substring(cs.indexOf('&')+1).split('&');
        //var pages = urlParam[1].split('=');

        var date = $('select[name="date"]').val(); //查询日期
        var module = $('select[name="module"]').val(); //类型
        var user = $('select[name="user"]').val(); //用户
        var pageSize = parseInt($('#list .number').text(), 10);

        url = url + '?'+cs[1];

        var param = {};
        param.date = date;
        param.module = module;
        param.user = user;
        param.pageSize = pageSize;
        param.package_id = package_id;

        package_update_log_page(url, param, '');
    });

    /****************  资产包日志分页end *****************/
});

function package_update_log_page(url, param, page) {
    $.ajax({
        type: "GET",//类型 POST GET
        url: url,
        data: param,
        datatype: "json",// 返回数据的格式"xml", "html", "script", "json", "jsonp", "text".
        // 在请求之前调用的函数
        // beforeSend: function () {

        // },
        // 调用执行后调用的函数
        // complete: function (XMLHttpRequest, textStatus) {

        // },
        //成功返回之后调用的函数
        success: function (data) {
            if (data.code == 200) {
                //分页
                $('#page_list').children().remove();
                if (data.data.res.count > 0) {
                    $('.pagingBox').show();
                    $('#page_list').html(data.data.res.pager);
                    $('.pagingBox').find('.totalSize').text(data.data.res.count);
                } else {
                    $('.pagingBox').hide();
                    $('.pagingBox').find('.totalSize').text(0);
                }

                //列表数据
                $('#page_list_tr').children().remove();
                var html = '';
                var pageSize = parseInt($('#list .number').text(), 10);
                var page = data.data.res.page;
                var j = pageSize * (page-1);
                if (data.data.res.list.length>0) {
                    $.each(data.data.res.list, function (i, n) {
                        html += '<tr>' +
                            //'<td class="text-center" style="width: 10%;">' + n.id + '</td>' +
                            '<td class="text-center" style="width: 10%;">' + (++j) + '</td>' +
                            '<td class="text-center" style="width: 15%;">' + n.created_at + '</td>' +
                            '<td style="width: 10%;"><span class="txt-cut">' + n.user_id + '</span></td>' +
                            '<td style="width: 15%;">' + n.module + '</td>' +
                            '<td style="width: 40%;"><span class="txt-cut" title="'+ n.log +'">' + n.log2 + '</span></td>' +
                            '</tr>';
                    });
                } else {
                    html += '<tr style="height: 51px;">'+
                        '<td class="text-center" style="width: 10%;"></td>' +
                        '<td class="text-center" style="width: 15%;"></td>' +
                        '<td style="width: 10%;"></td>' +
                        '<td style="width: 15%;"></td>' +
                        '<td style="width: 30%;"><span title=""></span></td>' +
                        '</tr> ' +
                        '<tr style="height: 51px;">'+
                        '<td class="text-center" style="width: 10%;"></td>' +
                        '<td class="text-center" style="width: 15%;"></td>' +
                        '<td style="width: 10%;"></td>' +
                        '<td style="width: 10%;"></td>' +
                        '<td style="width: 40%;"><span title=""></span></td>' +
                        '</tr>';
                }

                $('#page_list_tr').append(html);
                $('#current_page').val(data.data.res.page);
            }
        },
        //调用出错执行的函数
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.msg('系统错误');
        }
    });
}